package catchData;

import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.log4j.Logger;

import util.CatchDataUtil;

/**
 * @author suwy
 * @date 2014-2-14
 * @todo TODO
 */
public class CatcheNationalData4 {
	private static final Logger LOG = Logger.getLogger(CatcheNationalData4.class);
	private static String secondURL = "http://data.stats.gov.cn/quotas/getchildren?dbcode=hgnd&dimension=zb";
	private static String level3 = "&level=3&code=";
	
	private void excute() {
		HttpClient client = CatchDataUtil.initHttpClient();
		GetMethod secondGet = null;
		String[] codes = {"A010301",//有三级目录
				"A010302",
				"A010303",
				"A010401",
				"A010402",
				"A010501",
				"A010502",
				"A010503",
				"A010504",
				"A010505",
				"A010506",
				"A010507",
				"A010508",
				"A010509",
				"A01050A",
				"A01050B",
				"A01050C",
				"A01050D",
				"A01050E",
				"A01050F",
				"A01050G",
				"A01050H",
				"A020301",
				"A020302",
				"A020401",
				"A020402",
				"A020501",
				"A020502",
				"A020B01",
				"A020B02",
				"A020C01",
				"A020C02",
				"A020C03",
				"A020C04",
				"A020C05",
				"A020C06",
				"A020C07",
				"A020C08",
				"A020C09",
				"A020C0A",
				"A020C0B",
				"A020C0C",
				"A020C0D",
				"A020C0E",
				"A020C0F",
				"A020C0G",
				"A020C0H",
				"A020D01",
				"A020D02",
				"A020D03",
				"A020D04",
				"A020D05",
				"A020D06",
				"A020D07",
				"A020D08",
				"A020D09",
				"A020D0A",
				"A020D0B",
				"A020D0C",
				"A020D0D",
				"A020D0E",
				"A020D0F",
				"A020D0G",
				"A020D0H",
				"A020E01",
				"A020E02",
				"A020E03",
				"A020E04",
				"A020E05",
				"A020E06",
				"A020E07",
				"A020F01",
				"A020F02",
				"A020F03",
				"A020F04",
				"A020F05",
				"A020F06",
				"A020F07",
				"A020G01",
				"A020G02",
				"A020G03",
				"A030701",
				"A030702",
				"A030703",
				"A030704",
				"A030705",
				"A030706",
				"A030707",
				"A030708",
				"A030709",
				"A03070A",
				"A03070B",
				"A03070C",
				"A03070D",
				"A03070E",
				"A03070F",
				"A03070G",
				"A03070H",
				"A03070I",
				"A03070J",
				"A03070K",
				"A090301",
				"A090302",
				"A090303",
				"A090304",
				"A090305",
				"A090306",
				"A090307",
				"A090308",
				"A090401",
				"A090402",
				"A090403",
				"A090404",
				"A090405",
				"A090406",
				"A090407",
				"A090408",
				"A090501",
				"A090502",
				"A090503",
				"A090504",
				"A090505",
				"A090506",
				"A090507",
				"A090508",
				"A0A0405",
				"A0A0406",
				"A0A0408",
				"A0A0501",
				"A0A0502",
				"A0A0503",
				"A0A0504",
				"A0A0505",
				"A0A0506",
				"A0A0507",
				"A0A0508",
				"A0A0509",
				"A0A0D01",
				"A0A0D02",
				"A0A0D03",
				"A0A0D08",
				"A0A0D09",
				"A0A0D0A",
				"A0A0D0B",
				"A0A0D0C",
				"A0A0E01",
				"A0A0E02",
				"A0A0E03",
				"A0A0E04",
				"A0A0E05",
				"A0A0K01",
				"A0A0K02"};
		StringBuilder stringBuilder = new StringBuilder();
		for(int i = 0; i < codes.length; i++) {
			secondGet = new GetMethod(secondURL + level3 + codes[i]);
			CatchDataUtil.setHeaders(secondGet, secondURL + level3 + codes[i],
					CatchDataUtil.getWebSite(secondURL + level3 + codes[i]));
//			LOG.info(secondURL + level3 + codes[i]);
			try {
				client.executeMethod(secondGet);
				stringBuilder.append(secondGet.getResponseBodyAsString()
						.substring(1, secondGet.getResponseBodyAsString().length()-1) + ",");
			} catch (HttpException e) {
				LOG.info(e);
			} catch (IOException e) {
				LOG.info(e);
			} finally {
				secondGet.releaseConnection();
			}
		}
		LOG.info(stringBuilder);
	}
	
	public static void main(String[] args) {
		CatcheNationalData4 instance = new CatcheNationalData4();
		instance.excute();
	}

}
